package com.shou.infrastructure.persistent.repository.fee;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.fee.model.FeeScheduleEntity;
import com.shou.domain.fee.repository.IFeeScheduleRepository;
import com.shou.infrastructure.persistent.dao.IFeeScheduleDao;
import com.shou.infrastructure.persistent.po.FeeSchedulePO;
import com.shou.infrastructure.persistent.repository.fee.mapstruct.FeeSchedulePOConvert;
import jakarta.annotation.Resource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
 * ClassName:FeeScheduleRepository
 * Project:SmartPark
 * Package: com.shou.infrastructure.persistent.repository.fee
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/7 23:10
 * @Version 1.0
 */
@Repository("feeScheduleRepository")
public class FeeScheduleRepository implements IFeeScheduleRepository {
    @Resource
    private IFeeScheduleDao feeScheduleDao;
    @Resource
    private FeeSchedulePOConvert feeSchedulePOConvert;
    @Override
    public void insert(FeeScheduleEntity feeSchedule) {
        FeeSchedulePO feeSchedulePO = feeSchedulePOConvert.convertFeeScheduleEntityToPO(feeSchedule);
        try {
            feeScheduleDao.insertSelective(feeSchedulePO);
        } catch (DataIntegrityViolationException e) {
            Throwable rootCause = e.getRootCause();
            if (rootCause instanceof SQLException && "22001".equals(((SQLException) rootCause).getSQLState())) {
                throw new IllegalArgumentException("输入数据过长导致SQL执行失败: " + rootCause.getMessage());
            } else {
                throw new IllegalArgumentException(e.getMessage());
            }
        }
    }

    @Override
    public void updateFeeScheduleById(FeeScheduleEntity feeSchedule) {
        FeeSchedulePO feeSchedulePO = feeSchedulePOConvert.convertFeeScheduleEntityToPO(feeSchedule);
        feeScheduleDao.updateByPrimaryKeySelective(feeSchedulePO);
    }

    @Override
    public FeeScheduleEntity getFeeScheduleById(Integer id) {
        FeeSchedulePO feeSchedulePO = feeScheduleDao.selectById(id);
        return feeSchedulePOConvert.convertFeeSchedulePOToEntity(feeSchedulePO);
    }

    @Override
    public IPage<FeeScheduleEntity> getFeeSchedulesPage(PageQueryEntity pageQueryEntity) {
        // 根据页码和页面大小,构建分页对象
        Page<FeeSchedulePO> page = new Page<>(pageQueryEntity.getPageIndex(), pageQueryEntity.getPageSize());
        // 采用模糊查询
        IPage<FeeSchedulePO> pageMessage = feeScheduleDao.selectPageMessageByCondition(
                page,
                pageQueryEntity.getDescription(),
                pageQueryEntity.getHourlyRate(),
                pageQueryEntity.getCreateTime(),
                pageQueryEntity.getUpdateTime(),
                pageQueryEntity.getMemberDiscount(),
                pageQueryEntity.getIsAvailable(),
                pageQueryEntity.getFeeScheduleCode(),
                pageQueryEntity.getSortName(),
                pageQueryEntity.getSort()
        );
        return feeSchedulePOConvert.convertFeeSchedulePOPageToEntity(pageMessage);
    }
}
